<?php

/**
 * Controller_special
 */

class Controller_special extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');

	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 特殊课堂列表 **/
	public function actionList()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_special`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$special_lists = $this->db->fetchAll("SELECT * FROM `tbl_special` ORDER BY special_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($special_lists as $key => $value){
			$special_lists[$key]['type_note'] = $this->special_type_ini[$value['special_type']] . $value['type_name'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("special_lists", $special_lists);
		$this->tpl->display('special/special.list.tpl.php');
	}

	/** 特殊课堂添加 **/
	public function actionAdd()
	{
		if ($_POST){
			$special_name     	    = $this->post->special_name;
			$s_type     	        = $this->post->s_type;

			$type = substr($s_type, 0, 2);
			switch ($type){
				case "s_":
					$special_type = "school";
					$type_id = substr($s_type, 2);
					break;
				case "g_":
					$special_type = "grade";
					$type_id      = substr($s_type, 2);
					$grade_data   = $this->model->grade->getInfo($type_id);
					$type_name    = $grade_data['grade_name'];
					break;
				case "c_":
					$special_type = "class";
					$type_id      = substr($s_type, 2);
					$class_data   = $this->model->class->getInfo($type_id);
					$type_name    = $class_data['class_name'];
					break;
				default:
					$special_type = "school";
					$type_id = substr($s_type, 2);
					break;
			}

			$info = array(
			"special_name" => $special_name,
			"special_type" => $special_type,
			"type_id"      => $type_id,
			"type_name"    => $type_name,
			"status"       => 1,
			"create_time"  => date("Y-m-d H:i:s"),
			"update_time"  => "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_special", $info);


			// 跳转
			Helper_Client::location("添加成功", "index.php?a=special.list");
		}

		$class_lists = $this->model->class->getTree();

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('special/special.add.tpl.php');
	}
	/** 特殊课堂修改 **/
	public function actionUpdate()
	{

		$special_id   = (int)$this->get->special_id;
		$special_data = $this->db->fetchRow("SELECT * FROM `tbl_special` WHERE special_id='{$special_id}'");

		if (empty($special_data)){
			Helper_Client::alert("课堂不存在");
		}
		if ($_POST){
			$special_name     	    = $this->post->special_name;
			$s_type     	        = $this->post->s_type;

			$type = substr($s_type, 0, 2);
			switch ($type){
				case "s_":
					$special_type = "school";
					$type_id = substr($s_type, 2);
					break;
				case "g_":
					$special_type = "grade";
					$type_id      = substr($s_type, 2);
					$grade_data   = $this->model->grade->getInfo($type_id);
					$type_name    = $grade_data['grade_name'];
					break;
				case "c_":
					$special_type = "class";
					$type_id      = substr($s_type, 2);
					$class_data   = $this->model->class->getInfo($type_id);
					$type_name    = $class_data['class_name'];
					break;
				default:
					$special_type = "school";
					$type_id = substr($s_type, 2);
					break;
			}

			$info = array(
			"special_name" => $special_name,
			"special_type" => $special_type,
			"type_id"      => $type_id,
			"type_name"    => $type_name,
			"update_time"  => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_special", $info, "special_id='{$special_id}'");


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=special.list");
		}

		$class_lists = $this->model->class->getTree();

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("special_data", $special_data);
		$this->tpl->display('special/special.update.tpl.php');
	}

	/** 特殊课堂删除 **/
	public function actionDel()
	{
		$special_id = $this->get->special_id;
		$this->db->query("DELETE  FROM `tbl_special` WHERE special_id='{$special_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=special.list");
	}




}

?>